using System;
using System.Collections.Generic;
using System.Web.UI.WebControls;
using Guestbook.domain;

namespace Guestbook.Page
{
    public class GuestBookPage : BasePage
    {
        #region - Fields & Properties -

        protected System.Web.UI.WebControls.Literal lblTitle;
        protected System.Web.UI.WebControls.Literal lblTotalVisiters;
        protected System.Web.UI.WebControls.Literal lblTotalMessages;
        protected System.Web.UI.WebControls.Literal lblLastPost;

        protected System.Web.UI.WebControls.Literal lblPageSize;
        protected System.Web.UI.WebControls.Literal lblFrom;
        protected System.Web.UI.WebControls.Literal lblTo;

        protected System.Web.UI.WebControls.Repeater Repeater1;

        protected System.Web.UI.WebControls.HyperLink lnkViewAll;
        protected System.Web.UI.WebControls.HyperLink lnkLogin;

        protected System.Web.UI.WebControls.TextBox txtUserName;
        protected System.Web.UI.WebControls.TextBox txtMesage;
        protected System.Web.UI.WebControls.TextBox txtEmail;
        protected System.Web.UI.WebControls.TextBox txtWebsite;
        protected System.Web.UI.WebControls.Button btnPost;

        public int lPageSize = 10;

        private Guestbook.domain.GuestBook _guestBook;

        public int CurrentPage
        {
            get
            {
                string objPage = Request["page"];
                return objPage == null ? 1 : int.Parse(objPage);
            }
        }

        public int GuestBookId
        {
            get
            {
                string objPage = Request["id"];
                return int.Parse(objPage);
            }
        }

        #endregion

        protected void Page_Load(object sender, EventArgs e)
        {
            if (!this.IsPostBack)
            {
                btnPost.Attributes.Add("onclick", "return checkform();");

                try
                {
                    bool isExist = Guestbook.business.service.GuestBookService.IsExist(this.GuestBookId);
                    if (isExist)
                    {
                        lnkViewAll.NavigateUrl = string.Format("/guestbook.aspx?id={0}&page=-1", this.GuestBookId);
                        lnkLogin.NavigateUrl = string.Format("/admin/login.aspx?id={0}", this.GuestBookId);

                        _guestBook
                            = Guestbook.domain.GuestBook.Load(this.GuestBookId, 10, this.CurrentPage);

                        if (_guestBook != null)
                        {
                            int intFrom, intTo;
                            intFrom = (this.CurrentPage - 1) * lPageSize;
                            intTo = intFrom + this.lPageSize;
                            intTo = intTo > _guestBook.TotalMessages ?
                                _guestBook.TotalMessages : intTo;

                            lblPageSize.Text = lPageSize.ToString();
                            lblFrom.Text = intFrom.ToString("N0");
                            lblTo.Text = intTo.ToString("N0");

                            lblLastPost.Text = _guestBook.LastMessagePosted.ToUniversalTime().ToString();
                            lblTitle.Text = _guestBook.Title;
                            lblTotalMessages.Text = _guestBook.TotalMessages.ToString("N0");
                            lblTotalVisiters.Text = _guestBook.TotalVisitors.ToString("N0");

                            //PagedDataSource objPds = new PagedDataSource();
                            //objPds.DataSource = _guestBook.Messages;

                            //if (this.CurrentPage != -1)
                            //{
                            //    objPds.AllowPaging = true;
                            //    objPds.PageSize = 10;
                            //    objPds.CurrentPageIndex = this.CurrentPage - 1;
                            //}

                            Repeater1.DataSource = _guestBook.Messages;
                            Repeater1.DataBind();
                        }
                    }
                    else
                        throw new Exception();
                }
                catch (Exception)
                {
                    Response.Redirect("/");
                }
            }
        }

        protected void btnPost_Click(object sender, EventArgs e)
        {
            Message message = new Message();
            message.Active = true;
            message.Author = txtUserName.Text.Trim();
            message.Content = txtMesage.Text.Trim();
            message.DateCreation = DateTime.Now;
            message.UserEmail = txtEmail.Text.Trim();
            message.UserWebsite = txtWebsite.Text.Trim();
            message.ParentId = this.GuestBookId;

            if (message.IsValid)
            {
                message.Save();
                Response.Redirect(string.Format("~/guestbook.aspx?id={0}&page={1}",
                    this.GuestBookId, this.CurrentPage));
            }
        }

        protected void repTable_ItemDataBound(object sender, RepeaterItemEventArgs e)
        {
            if (e.Item.ItemType == ListItemType.Footer)
            {
                //Repeater repPager = e.Item.FindControl("repPager") as Repeater;
                //repPager.ItemDataBound += new RepeaterItemEventHandler(repPager_ItemDataBound);

                //How many pages?
                int mod = _guestBook.TotalMessages % lPageSize;
                int lPageCount = Convert.ToInt32(_guestBook.TotalMessages / lPageSize) + 1;
                if (mod > 0)
                    lPageCount += 1;

                //Create a list
                System.Collections.Generic.List<int> listPages
                    = new System.Collections.Generic.List<int>();
                for (int i = 1; i < lPageCount; i++)
                    listPages.Add(i);

                //repPager.DataSource = listPages;
                //repPager.DataBind();

                if (this.CurrentPage + 1 != lPageCount & this.CurrentPage != -1)
                {
                    HyperLink lnkNextPage = e.Item.FindControl("lnkNextPage") as HyperLink;
                    lnkNextPage.NavigateUrl = string.Format("~/guestbook.aspx?id={0}&page={1}",
                        this.GuestBookId, this.CurrentPage + 1);
                }

                HyperLink lnkAllPage = e.Item.FindControl("lnkAllPage") as HyperLink;
                lnkAllPage.NavigateUrl = string.Format("~/guestbook.aspx?id={0}&page={1}",
                    this.GuestBookId, -1);

                DropDownList ddlPages = e.Item.FindControl("ddlPages") as DropDownList;
                ddlPages.DataSource = listPages;
                ddlPages.DataBind();
                ddlPages.Text = this.CurrentPage.ToString();
            }
        }

        protected void ddlPages_SelectedIndexChanged(object sender, EventArgs e)
        {
            try
            {
                string selectedPage = (sender as DropDownList).Text;
                Response.Redirect(string.Format("~/guestbook.aspx?id={0}&page={1}", this.GuestBookId, selectedPage), false);
            }
            catch (Exception)
            {
                //todo
            }
        }
    }
}
